%eyelink2_behavioral_anaysis

clc
clear
close all
scnsize = get(0,'ScreenSize');
graph_suru=0;
all_file_names=[''];
eyelink.subject=[];  
eyelink.quad=[];
eyelink.anti=[];
eyelink.peri=[];
eyelink.srt=[];
eyelink.rank_srt=[];
eyelink.error=[];
eyelink.delay=[];
eyelink.long=[];
eyelink.sac_thres=[];
srt_pro=[];
srt_anti=[];
srt_error=[];
srt_pro_peri=[];
srt_anti_peri=[];
srt_error_peri=[];
srt_pro_cent=[];
srt_anti_cent=[];
srt_error_cent=[];
srt_pro_pro  =[];
srt_anti_anti=[];
srt_anti_pro =[];
srt_pro_anti=[];
fig(220);clf;

cd 'C:\_bcoe\eyelink2_data'
folders = dir;
fred=[];
for i=1:length(folders)
    fred(i)=isdir(folders(i).name) & length(folders(i).name)>2; 
end
folders(~fred)=[];
clear fred;index=1;
for i=1:length(folders)
   cd(folders(i).name);
   if i==1
       subject=folders(i).name(1:2);
   else
       if any(subject(index,:)~=folders(i).name(1:2))
           index=index+1;
           subject(index,:)=folders(i).name(1:2);
       end
   end
%     subject(i,:)=folders(i).name([1:2,11, 9:13]);

    files = dir('*C.mat'); % find all files with the eye extention
    files.name;
    if length(files)==0
        disp(' ');
        disp('there are no *C.mat files in this folder:')
        cd
        uimenufcn(gcf,'WindowCommandWindow');
        return
    end
    
    for fp_num=1:length(files)
        load(files(fp_num).name);
         
%         file_name=C.name;
%         file_name(3)='_'
%         all_file_names(end+1,:)=file_name;
%         eval([file_name '=C;'])
        eyelink.subjectid(index,:)=subject(index,:);
        eyelink.subject=[eyelink.subject; index*ones(size(C.anti))];
        eyelink.anti   =[eyelink.anti;    C.anti];
        eyelink.peri   =[eyelink.peri;    C.peri];
        eyelink.srt    =[eyelink.srt;     C.srt'];
        eyelink.error  =[eyelink.error;   C.error'];
        eyelink.delay  =[eyelink.delay;   C.delay];
        eyelink.long   =[eyelink.long;    diff(C.timeb(:,1))>10000];
        eyelink.sac_thres  =[eyelink.sac_thres;   C.sac_thres'];

        
        eyelink.quad=[ eyelink.quad; C.quad'];
        fred=group_rank(C.srt(C.srt>0))'
        clear rank_srt
        rank_srt(C.srt>0,:)=fred(:,3);
        eyelink.rank_srt=[eyelink.rank_srt; rank_srt];
        
        srt_pro =[srt_pro  C.srt(C.anti==0 & C.error'==0 )];
        srt_anti=[srt_anti C.srt(C.anti==1 & C.error'==0 )];
        srt_error=[srt_error C.srt(C.error'==3 )];
        
%         if mean(diff(C.iti))'>10000
%             fig(220);hold on;
%         else
%             fig(221);hold on;
%         end
%         h=text( index*ones(size(C.srt(C.anti==0 & C.error'==0 ))) -.1,C.srt(C.anti==0 & C.error'==0 ),'p');
%         set(h,'color',C.colors.PRO);
%         h=text([index*ones(size(C.srt(C.anti==1 & C.error'==0 )))]+.1,C.srt(C.anti==1 & C.error'==0 ),'a');
%         set(h,'color',C.colors.ANTI);

        ylim([250 1000]);
        xlim([.5 i+.5]);
        srt_pro_peri =[srt_pro_peri  C.srt(C.anti==0 & C.error'==0 & C.peri==1)];
        srt_anti_peri=[srt_anti_peri C.srt(C.anti==1 & C.error'==0 & C.peri==1)];
        srt_error_peri=[srt_error_peri C.srt(C.error'==3 & C.peri==1)];
        srt_pro_cent =[srt_pro_cent  C.srt(C.anti==0 & C.error'==0 & C.peri==0)];
        srt_anti_cent=[srt_anti_cent C.srt(C.anti==1 & C.error'==0 & C.peri==0)];
        srt_error_cent=[srt_error_cent C.srt(C.error'==3 & C.peri==0)];
        % 1-back crap
        srt_pro_pro  =[srt_pro_pro   C.srt([0; [C.anti(1:end-1)==0 &C.anti(2:end)==0 & C.error(2:end)'==0]]>0 )];
        srt_anti_anti=[srt_anti_anti C.srt([0; [C.anti(1:end-1)==1 &C.anti(2:end)==1 & C.error(2:end)'==0]]>0 )];
        srt_anti_pro =[srt_anti_pro  C.srt([0; [C.anti(1:end-1)==1 &C.anti(2:end)==0 & C.error(2:end)'==0]]>0 )];
        srt_pro_anti =[srt_pro_anti  C.srt([0; [C.anti(1:end-1)==0 &C.anti(2:end)==1 & C.error(2:end)'==0]]>0 )];
        
    end
%     fig(20); 
%     xlim([i-.5 i+.5]);
%     vline(i);hold on;
%     [cnt val]=hist_b(eyelink.srt(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==1),25)
%     cnt_a=cnt/40;
%     hplot_v(val,-cnt_a+i,'vb',i)
%     [cnt val]=hist_b(eyelink.srt(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==1),25)
%     cnt_a=cnt/40;
%     hplot_v(val,cnt_a+i,'vr',i) 
%     hline(mean(eyelink.srt(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==1)),'b')
%     hline(mean(eyelink.srt(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==1)),'r')
%     set(gca,'xtick',[1:i]);
%     set(gca,'xticklabel',subject);
%     set(gcf,'name','Eyelink2 Behavioral Data - Long Trials')
%     xlim([.5 i+.5])
% 
%     fig(21); 
%     xlim([i-.5 i+.5])
%     vline(i);hold on;
%     [cnt val]=hist_b(eyelink.srt(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==0),25)
%     cnt_a=cnt/40;
%     hplot_v(val,-cnt_a+i,'vb',i)
%     [cnt val]=hist_b(eyelink.srt(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==0),25)
%     cnt_a=cnt/40;
%     hplot_v(val,cnt_a+i,'vr',i) 
%     hline(mean(eyelink.srt(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==0)),'b')
%     hline(mean(eyelink.srt(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==0)),'r')
%     set(gca,'xtick',[1:i]);
%     set(gca,'xticklabel',subject);
%     set(gcf,'name','Eyelink2 Behavioral Data - Short Trials')
%     xlim([.5 i+.5])
    cd ..
    
end

a=hist(srt_pro_peri  ,[300:25:800]-25);
b=hist(srt_anti_peri ,[300:25:800]-25);
c=hist(srt_error_peri,[300:25:800]-25);
d=hist(srt_pro_cent  ,[300:25:800]-25);
e=hist(srt_anti_cent ,[300:25:800]-25);
f=hist(srt_error_cent,[300:25:800]-25);

fig(201),clf;
bar([300:25:800],[a; b; c; d; e; f; ]');
hold
hplot([300:25:800]-8,a,'color',[0 0 .6])
hplot([300:25:800]-5,b,'color',[.2 .3 1])
hplot([300:25:800]-1.5,c,'color',[0 1 1])
hplot([300:25:800]+1.50,d,'color',[1 1 0])
hplot([300:25:800]+5,e,'color',[1 .3 0])
hplot([300:25:800]+8,f,'color',[.5 0 0])
h=text( 300,60-0 ,'srt-pro-peri');set(h,'color',[0 0 .6])
h=text( 300,60-2 ,'srt-anti-peri');set(h,'color',[.2 .3 1])
h=text( 300,60-4 ,'srt-error-peri');set(h,'color',[0 1 1])
h=text( 300,60-6 ,'srt-pro-cent');set(h,'color',[1 1 0])
h=text( 300,60-8 ,'srt-anti-cent');set(h,'color',[1 .3 0])
h=text( 300,60-10,'srt-error-cent');set(h,'color',[.5 0 0])
axis tight

fig(202),clf,set(gcf,'name','PERI v CENT (pro/anti/error)')
subplot(2,3,1),ttest_bcoe(srt_pro_peri,srt_pro_cent)
subplot(2,3,2),ttest_bcoe(srt_anti_peri,srt_anti_cent)
subplot(2,3,3),ttest_bcoe(srt_error_peri,srt_error_cent)
subplot(2,3,4),mann_whitney(srt_pro_peri,srt_pro_cent)
subplot(2,3,5),mann_whitney(srt_anti_peri,srt_anti_cent)
subplot(2,3,6),mann_whitney(srt_error_peri,srt_error_cent)



srt_pro_pro  =[srt_pro_pro   C.srt([0; [C.anti(1:end-1)==0 &C.anti(2:end)==0 & C.error(2:end)'==0]]>0 )];
srt_anti_anti=[srt_anti_anti C.srt([0; [C.anti(1:end-1)==1 &C.anti(2:end)==1 & C.error(2:end)'==0]]>0 )];
srt_anti_pro =[srt_anti_pro  C.srt([0; [C.anti(1:end-1)==1 &C.anti(2:end)==0 & C.error(2:end)'==0]]>0 )];
srt_pro_anti =[srt_pro_anti  C.srt([0; [C.anti(1:end-1)==0 &C.anti(2:end)==1 & C.error(2:end)'==0]]>0 )];
fig(204),clf;set(gcf,'name','Eyelink2 - Population Pro v Anti ')
subplot(2,1,1);hold on;axis([.5 2.5 550 650]);tickout
ylabel('Saccadic Reaction Time')
h=text(2,mean(srt_pro_peri),'peri');set(h,'color',[0 0 0],'FontWeight','bold');
h=text(2,mean(srt_pro_cent),'cent');set(h,'color',[0.8 0.8 0.8],'FontWeight','bold');
hline(mean(srt_pro),[.5 .5 .8]);
h=text(2,mean(srt_pro),'pro');set(h,'color',[0 0 1],'FontWeight','bold');

h=text(2,mean(srt_anti_peri),'peri');set(h,'color',[0 0 0],'FontWeight','bold');
h=text(2,mean(srt_anti_cent),'cent');set(h,'color',[0.8 0.8 0.8],'FontWeight','bold');
hline(mean(srt_anti),[.8 .5 .5]);
h=text(2,mean(srt_anti),'anti');set(h,'color',[ 1 0 0],'FontWeight','bold');

h=text(1,mean(srt_pro_pro),'pro->pro');set(h,'color',[0 0 1],'FontWeight','bold');
h=text(1,mean(srt_anti_pro),'anti->pro');set(h,'color',[.5 0 1],'FontWeight','bold');
h=text(1,mean(srt_pro_anti),'pro->anti');set(h,'color',[1 0 .5],'FontWeight','bold');
h=text(1,mean(srt_anti_anti),'anti->anti');set(h,'color',[1 0 0],'FontWeight','bold');

xlim([.5 2.5]);
subplot(2,2,3);
ttest_bcoe(srt_anti,srt_pro);
subplot(2,2,4);
mann_whitney(srt_anti,srt_pro);





fig(220);clf;hold on;ylim([200 1200])
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 scnsize(4)/2-30 0 0]));
set(gcf,'Name','Long Trials');tickout;
Ylabel('Saccadic Reaction Time')
for i = 1:length(subject);
    xlim([i-.5 i+.5]);
    vline(i);
    [cnt val]=hist_b(eyelink.srt(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==1),25);
    cnt_a=cnt/40;
    hplot_v(val,-cnt_a+i,'vb',i);
    [cnt val]=hist_b(eyelink.srt(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==1),25);
    cnt_a=cnt/40;
    hplot_v(val,cnt_a+i,'vr',i);
    hline(mean(eyelink.srt(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==1)),'b')
    hline(mean(eyelink.srt(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==1)),'r')
end
set(gca,'xtick',[1:index]);
set(gca,'xticklabel',subject);
set(gcf,'name','Eyelink2 Behavioral Data - Long Trials')
axis auto;ylim([200 1200])

fig(320);clf;hold on;ylim([200 1200])
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 scnsize(4)/2-30 0 0]));
set(gcf,'Name','Short Trials');
Ylabel('Saccadic Reaction Time')
for i = 1:length(subject);
    xlim([i-.5 i+.5])
    vline(i);
    [cnt val]=hist_b(eyelink.srt(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==0),25);
    cnt_a=cnt/30;
    hplot_v(val,-cnt_a+i,'vb',i);
    [cnt val]=hist_b(eyelink.srt(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==0),25);
    cnt_a=cnt/30;
    hplot_v(val,cnt_a+i,'vr',i);
    hline(mean(eyelink.srt(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==0)),'b')
    hline(mean(eyelink.srt(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==0)),'r')
end
set(gca,'xtick',[1:index]);
set(gca,'xticklabel',subject);
set(gcf,'name','Eyelink2 Behavioral Data - Short Trials')
axis auto;ylim([200 1200])

warning off MATLAB:divideByZero
fig(330);clf;set(gcf,'name','Short Trials')
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 30 0 0]));
for i = 1:max(index);
    pro =eyelink.srt(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==0);
    anti=eyelink.srt(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==0);
    subplot(2,max(index),i);mann_whitney(anti,pro); 
    subplot(2,max(index),i+max(index));ttest_bcoe(anti,pro) ;
end
fig(330);fig(220);

fig(230);clf;set(gcf,'name','Long Trials')
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 30 0 0]));
for i = 1:max(i);
    pro =eyelink.srt(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==1);
    anti=eyelink.srt(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==1);
    subplot(2,max(index),i);mann_whitney(anti,pro);
    subplot(2,max(index),i+max(index));ttest_bcoe(anti,pro) ;
end
fig(230);fig(220);


fig(240);clf;
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 30 0 0]));
subplot(2,1,1);tickout;title('Short Trials');
fred=[];
for i = 1:max(i)
    pro_o =sum(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==0);
    pro_x =sum(eyelink.subject==i & eyelink.anti==0 & eyelink.error==3 & eyelink.long==0);
    anti_o=sum(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==0);
    anti_x=sum(eyelink.subject==i & eyelink.anti==1 & eyelink.error==3 & eyelink.long==0);
    nb_trials=sum(eyelink.subject==i & eyelink.anti==0 & eyelink.long==0);
    fred=[fred; pro_o/nb_trials pro_x/nb_trials anti_o/nb_trials anti_x/nb_trials] ;
end
bar(fred);
set(gca,'xtick',[1:i]);
set(gca,'xticklabel',subject);
set(gcf,'name','EyelinkII Behavioral Data')
Xlabel('Subject')
Ylabel('% Correct/Incorrect')


subplot(2,1,2);tickout;title('Long Trials')
fred=[];
for i = 1:max(index)
    pro_o =sum(eyelink.subject==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==1);
    pro_x =sum(eyelink.subject==i & eyelink.anti==0 & eyelink.error==3 & eyelink.long==1);
    anti_o=sum(eyelink.subject==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==1);
    anti_x=sum(eyelink.subject==i & eyelink.anti==1 & eyelink.error==3 & eyelink.long==1);
    nb_trials=sum(eyelink.subject==i & eyelink.anti==0 & eyelink.long==1);
    fred=[fred; pro_o/nb_trials pro_x/nb_trials anti_o/nb_trials anti_x/nb_trials] ;
end
bar(fred);
set(gca,'xtick',[1:index]);
set(gca,'xticklabel',subject);
warning on MATLAB:divideByZero
set(gcf,'name','EyelinkII Behavioral Data')
Xlabel('Subject')
Ylabel('% Correct/Incorrect')





fig(206),clf;set(gcf,'name','Delay effect on SRT - long (culmulative)')
fig(205),clf;set(gcf,'name','Delay effect on SRT - long')
subplot(3,1,1);hold on;title('Delay effect on SRT (long trials)')
for i=0:.5:1
    subplot(3,1,1);tickout
    pro_fred=eyelink.srt(eyelink.anti==0 & eyelink.error==0 & eyelink.delay==i  & eyelink.long==1 )
    xlim([i-.25 i+.25]);
    hline(mean(pro_fred),'b')
%     h=text(ones(size(pro_fred))*i-.04, pro_fred,'p');
%     set(h,'color',[0 0 1],'FontWeight','bold')
    [cnt val]=hist_b(pro_fred,25)
    cnt_a=cnt/200;
    hplot_v(val,-cnt_a+i,'vb',i)
    fig(206);
    hplot(val,(cumsum(cnt)/sum(cnt)*.5)+i,'b')
    fig(205);

    anti_fred=eyelink.srt(eyelink.anti==1 & eyelink.error==0 & eyelink.delay==i & eyelink.long==1 )
    hline(mean(anti_fred),'r')
%     h=text(ones(size(anti_fred))*i+.02, anti_fred,'a');
%     set(h,'color',[1 0 0],'FontWeight','bold')
    [cnt val]=hist_b(anti_fred,25)
    cnt_a=cnt/200;
    hplot_v(val,cnt_a+i,'vr',i)
    axis([-.25 1.25 200 1400])
    vline(i);
    fig(206);
    hplot(val,(cumsum(cnt)/sum(cnt)*.5)+i,'r')
    fig(205);
    
    subplot(3,3,(i/.5)+4);tickout
    ttest_bcoe(anti_fred,pro_fred)
    subplot(3,3,(i/.5)+7)
    mann_whitney(anti_fred,pro_fred)
end
fig(206);



fig(306),clf;set(gcf,'name','Delay effect on SRT - short (culmulative)')
fig(305),clf;set(gcf,'name','Delay effect on SRT - short')
subplot(3,1,1);hold on;title('Delay effect on SRT (short trials)')
for i=0:.5:1
    subplot(3,1,1);tickout
    pro_fred=eyelink.srt(eyelink.anti==0 & eyelink.error==0 & eyelink.delay==i  & eyelink.long==0 )
    xlim([i-.25 i+.25]);
    hline(mean(pro_fred),'b')
%     h=text(ones(size(pro_fred))*i-.04, pro_fred,'p');
%     set(h,'color',[0 0 1],'FontWeight','bold')
    [cnt val]=hist_b(pro_fred,25)
    cnt_a=cnt/200;
    hplot_v(val,-cnt_a+i,'vb',i)
    fig(306);
    hplot(val,(cumsum(cnt)/sum(cnt)*.5)+i,'b')
    fig(305);

    anti_fred=eyelink.srt(eyelink.anti==1 & eyelink.error==0 & eyelink.delay==i & eyelink.long==0 )
    hline(mean(anti_fred),'r')
%     h=text(ones(size(anti_fred))*i+.02, anti_fred,'a');
%     set(h,'color',[1 0 0],'FontWeight','bold')
    [cnt val]=hist_b(anti_fred,25)
    cnt_a=cnt/200;
    hplot_v(val,cnt_a+i,'vr',i)
    axis([-.25 1.25 200 1400])
    vline(i);
    fig(306);
    hplot(val,(cumsum(cnt)/sum(cnt)*.5)+i,'r')
    fig(305);
    
    subplot(3,3,(i/.5)+4);tickout
    ttest_bcoe(anti_fred,pro_fred)
    subplot(3,3,(i/.5)+7)
    mann_whitney(anti_fred,pro_fred)
end
fig(306);
save('eyelink','eyelink')